import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import SpringView from "@/views/SpringView.vue";
import TestAxiosView from "@/views/TestAxiosView";
import LoginView from "@/views/login/LoginView";
import RegisterView from "@/views/register/RegisterView";
import DisplayUser from "@/views/displayuser/DisplayUser";
import Index from "@/views/index/index";
import Qd from "@/views/Qd/Qd";
import Sm from "@/views/shuoming/Sm"

const routes = [
  {
    path: '/',
    // redirect:'/login'
    component:LoginView,
    meta:{
      loginRequire:false
    }
  },
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },{
  path: '/test',
    name:'test',
    component: () => import('../views/TestView.vue')
  },
  {
    path: '/spring',
    component: SpringView

  },
  {
    path:'/testaxios',
    component: TestAxiosView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta:{
      loginRequire:false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta:{
      loginRequire:false
    }
  },
  {
    path:'/list',
    name:DisplayUser,
    component: DisplayUser
  },
  {
    path:'/index',
    name:'Index',
    component: Index,
  },
  {
    path:'/qd',
    name:Qd,
    component: Qd
  },
  {
    path:'/sm',
    name:Sm,
    component: Sm
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
router.beforeEach((to,from,next) => {
  if(to.matched.some(function(item){
    console.log(item,"是否需要登录校验",item.meta.loginRequire)
    return item.meta.loginRequire
  })) {let loginUser = Tool.getLoginUser();
    if(Tool.isEmpty(loginUser)){
      console.log("用户未登录")
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});


export default router
